using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace TTBox
{
public class Memory
{
    // Fields
    private StreamReader fileSR;
    private StreamWriter fileWR;

    // Methods
    public void DeleteSavedFile(string filePath)
    {
        try
        {
            File.Delete(filePath);
        }
        catch (Exception)
        {
        }
        finally
        {
            filePath = "";
        }
    }

    public string GetDate()
    {
        string message = "";
        string dt = "";
        try
        {
            dt = GPSData.dt;
            message = "20" + dt.Substring(0, 2) + "/" + dt.Substring(2, 2) + "/" + dt.Substring(4, 2) + " ";
            dt = Convert.ToDateTime(message + dt.Substring(6, 2) + ":" + dt.Substring(8, 2) + ":" + dt.Substring(10, 2) + ".000").AddMinutes(480.0).ToString("yy-MM");
        }
        catch (Exception exception)
        {
            dt = "00-00";
            message = exception.Message;
        }
        return dt;
    }

    public bool GetDateDiff(string flag, string EndDT)
    {
        string message = "";
        string dt = "";
        bool flag2 = false;
        try
        {
            dt = GPSData.dt;
            message = "20" + dt.Substring(0, 2) + "/" + dt.Substring(2, 2) + "/" + dt.Substring(4, 2) + " ";
            DateTime time = Convert.ToDateTime(message + dt.Substring(6, 2) + ":" + dt.Substring(8, 2) + ":" + dt.Substring(10, 2)).AddMinutes(480.0);
            TimeSpan span = Convert.ToDateTime(EndDT).Subtract(time);
            if (flag == "S")
            {
                if (span.TotalMinutes <= 10.0)
                {
                    flag2 = true;
                }
                return flag2;
            }
            if (span.TotalMinutes <= 10.0)
            {
                flag2 = true;
            }
        }
        catch (Exception exception)
        {
            message = exception.Message;
        }
        finally
        {
            message = "";
            dt = "";
        }
        return flag2;
    }

    public string GetDateTime()
    {
        string message = "";
        string dt = "";
        try
        {
            dt = GPSData.dt;
            message = "20" + dt.Substring(0, 2) + "/" + dt.Substring(2, 2) + "/" + dt.Substring(4, 2) + " ";
            dt = Convert.ToDateTime(message + dt.Substring(6, 2) + ":" + dt.Substring(8, 2) + ":" + dt.Substring(10, 2) + ".000").AddMinutes(480.0).ToString("HH:mm");
        }
        catch (Exception exception)
        {
            dt = "00:00";
            message = exception.Message;
        }
        return dt;
    }

    public string GetMonthDay()
    {
        string message = "";
        string dt = "";
        try
        {
            dt = GPSData.dt;
            message = "20" + dt.Substring(0, 2) + "/" + dt.Substring(2, 2) + "/" + dt.Substring(4, 2) + " ";
            dt = Convert.ToDateTime(message + dt.Substring(6, 2) + ":" + dt.Substring(8, 2) + ":" + dt.Substring(10, 2) + ".000").AddMinutes(480.0).ToString("MM-dd");
        }
        catch (Exception exception)
        {
            dt = "00-00";
            message = exception.Message;
        }
        return dt;
    }

    public DateTime GetUTCDateTime()
    {
        string message = "";
        string dt = "";
        DateTime time = new DateTime();
        try
        {
            dt = GPSData.dt;
            message = "20" + dt.Substring(0, 2) + "/" + dt.Substring(2, 2) + "/" + dt.Substring(4, 2) + " ";
            time = Convert.ToDateTime(message + dt.Substring(6, 2) + ":" + dt.Substring(8, 2) + ":" + dt.Substring(10, 2));
            time = time.AddMinutes(480.0);
        }
        catch (Exception exception)
        {
            message = exception.Message;
        }
        return time;
    }

    public string ReadAutoQueueStatus(string filePath)
    {
        string message = "";
        try
        {
            if (File.Exists(filePath))
            {
                this.fileSR = new StreamReader(filePath);
                message = this.fileSR.ReadLine();
                this.fileSR.Close();
            }
        }
        catch (IOException exception)
        {
            message = exception.Message;
        }
        catch (Exception exception2)
        {
            message = exception2.Message;
        }
        finally
        {
            this.fileSR = null;
        }
        return message;
    }

    public string ReadDispatchStatusFromMemory(string filePath)
    {
        string str = "";
        string str2 = "";
        try
        {
            if (!File.Exists(filePath))
            {
                return str2;
            }
            this.fileSR = new StreamReader(filePath);
            while (!this.fileSR.EndOfStream)
            {
                str2 = this.fileSR.ReadLine() + "#";
                str2 = str2 + this.fileSR.ReadLine() + "#";
                str2 = str2 + this.fileSR.ReadLine() + "#";
                str2 = str2 + this.fileSR.ReadLine() + "#";
                str2 = str2 + this.fileSR.ReadLine() + "#";
                str2 = str2 + this.fileSR.ReadLine() + "#";
                for (str = this.fileSR.ReadLine(); str != "Done"; str = this.fileSR.ReadLine())
                {
                    str2 = str2 + str + "\r\n";
                }
            }
        }
        catch (IOException exception)
        {
            str = exception.Message + "\r\n" + exception.StackTrace;
        }
        catch (Exception exception2)
        {
            str = exception2.Message + "\r\n" + exception2.StackTrace;
        }
        finally
        {
            str = "";
            if (this.fileSR != null)
            {
                this.fileSR.Close();
                this.fileSR = null;
            }
        }
        return str2;
    }

    public DataTable ReadFromMemory(string filePath)
    {
        string message = "";
        DataTable table = null;
        try
        {
            if (!File.Exists(filePath))
            {
                return table;
            }
            table = new DataTable();
            table.Columns.Add("Record");
            table.Columns.Add("Status");
            this.fileSR = new StreamReader(filePath);
            while (!this.fileSR.EndOfStream)
            {
                string str2 = "";
                DataRow row = table.NewRow();
                message = this.fileSR.ReadLine();
                str2 = this.fileSR.ReadLine() + "\r\n";
                row[0] = message + " " + str2;
                message = message + "\r\n" + str2;
                for (str2 = this.fileSR.ReadLine(); str2 != "Done"; str2 = this.fileSR.ReadLine())
                {
                    message = message + str2 + "\r\n";
                }
                row[1] = message;
                table.Rows.Add(row);
                table.AcceptChanges();
                message = "";
            }
            this.fileSR.Close();
        }
        catch (IOException exception)
        {
            table = null;
            message = exception.Message;
        }
        catch (Exception exception2)
        {
            table = null;
            message = exception2.Message;
        }
        finally
        {
            this.fileSR = null;
        }
        return table;
    }

    public string ReadMessage()
    {
        string message = "";
        string path = "";
        try
        {
            path = "/FlashDisk/PNDSoftware/Message_Status.txt";
            if (File.Exists(path))
            {
                this.fileSR = new StreamReader(path);
                message = this.fileSR.ReadLine();
                this.fileSR.Close();
            }
        }
        catch (IOException exception)
        {
            message = "";
            message = exception.Message;
        }
        catch (Exception exception2)
        {
            message = "";
            message = exception2.Message;
        }
        finally
        {
            path = "";
            this.fileSR = null;
        }
        return message;
    }

    public string ReadPNDStatus(string filePath)
    {
        string message = "";
        try
        {
            if (File.Exists(filePath))
            {
                this.fileSR = new StreamReader(filePath);
                message = this.fileSR.ReadLine();
                this.fileSR.Close();
            }
        }
        catch (IOException exception)
        {
            message = exception.Message;
        }
        catch (Exception exception2)
        {
            message = exception2.Message;
        }
        finally
        {
            this.fileSR = null;
        }
        return message;
    }

    public DataTable ReadResDispatchFromMemory(string filePath)
    {
        string message = "";
        DataRow row = null;
        DataTable table = null;
        try
        {
            if (!File.Exists(filePath))
            {
                return table;
            }
            table = new DataTable();
            table.Columns.Add("SR");
            table.Columns.Add("Status");
            table.Columns.Add("Record");
            int num = 0;
            this.fileSR = new StreamReader(filePath);
            while (!this.fileSR.EndOfStream)
            {
                num++;
                row = table.NewRow();
                row[0] = num.ToString();
                message = this.fileSR.ReadLine();
                row[1] = message;
                message = "";
                string str2 = "";
                for (str2 = this.fileSR.ReadLine(); str2 != "Done"; str2 = this.fileSR.ReadLine())
                {
                    message = message + str2 + "\r\n";
                }
                row[2] = message;
                table.Rows.Add(row);
                table.AcceptChanges();
            }
            this.fileSR.Close();
        }
        catch (IOException exception)
        {
            message = exception.Message;
        }
        catch (Exception exception2)
        {
            message = exception2.Message;
        }
        finally
        {
            message = "";
            row = null;
            this.fileSR = null;
        }
        return table;
    }

    public void SaveDispatchStatusInMemory(string flagDispatch, string disProc, string disConfirm, string available, string Message)
    {
        string custReqId = "";
        string path = "";
        try
        {
            path = "/FlashDisk/PNDSoftware/Dispatch Record";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (flagDispatch == "0")
            {
                custReqId = GPSData.CustReqId;
                path = Path.Combine(path, "RealNDesignateDispatchStatus.txt");
            }
            else if (flagDispatch == "2")
            {
                custReqId = GPSData.CustShortReqId;
                path = Path.Combine(path, "ShortDispatchStatus.txt");
            }
            else if (flagDispatch == "3")
            {
                custReqId = GPSData.CustLongReqId;
                path = Path.Combine(path, "LongDispatchStatus.txt");
            }
            this.fileWR = new StreamWriter(path, false);
            this.fileWR.Write(flagDispatch + "\r\n");
            this.fileWR.Write(disProc + "\r\n");
            this.fileWR.Write(disConfirm + "\r\n");
            this.fileWR.Write(GPSData.DispatchProc + "\r\n");
            this.fileWR.Write(available + "\r\n");
            this.fileWR.Write(custReqId + "\r\n");
            this.fileWR.Write(Message + "\r\n");
            this.fileWR.Write("Done\r\n");
        }
        catch (IOException exception)
        {
            path = exception.Message;
        }
        catch (Exception exception2)
        {
            path = exception2.Message;
        }
        finally
        {
            this.fileWR.Flush();
            this.fileWR.Close();
            this.fileWR = null;
        }
    }

    public void WriteAutoQueueStatus(string status)
    {
        string message = "";
        try
        {
            message = "/FlashDisk/PNDSoftware";
            message = Path.Combine(message, "Auto_Queue_Status.txt");
            this.fileWR = new StreamWriter(message, false);
            this.fileWR.WriteLine(status);
        }
        catch (IOException exception)
        {
            message = exception.Message;
        }
        catch (Exception exception2)
        {
            message = exception2.Message;
        }
        finally
        {
            this.fileWR.Flush();
            this.fileWR.Close();
            this.fileWR = null;
        }
    }

    public void WriteIntoMemory(string msgDT, string Record)
    {
        string path = "";
        string date = "";
        try
        {
            date = this.GetDate();
            path = "/FlashDisk/PNDSoftware/Dispatch Record/" + date;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(path, date + "_Dispatch.txt");
            this.fileWR = new StreamWriter(path, true);
            this.fileWR.Write(" " + this.GetMonthDay() + " " + this.GetDateTime() + msgDT);
            this.fileWR.Write(" " + Record + "\r\n");
            this.fileWR.Write("Done\r\n");
        }
        catch (IOException exception)
        {
            date = exception.Message;
        }
        catch (Exception exception2)
        {
            date = exception2.Message;
        }
        finally
        {
            this.fileWR.Flush();
            this.fileWR.Close();
            this.fileWR = null;
        }
    }

    public void WriteMessage(string status)
    {
        string message = "";
        try
        {
            message = "/FlashDisk/PNDSoftware";
            message = Path.Combine(message, "Message_Status.txt");
            this.fileWR = new StreamWriter(message, false);
            this.fileWR.WriteLine(status);
        }
        catch (IOException exception)
        {
            message = exception.Message;
        }
        catch (Exception exception2)
        {
            message = exception2.Message;
        }
        finally
        {
            this.fileWR.Flush();
            this.fileWR.Close();
            this.fileWR = null;
        }
    }

    public void WriteMessageInMemory(string msgSubject, string msgBody)
    {
        string path = "";
        string date = "";
        try
        {
            date = this.GetDate();
            path = "/FlashDisk/PNDSoftware/Dispatch Record/" + date;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(path, date + "_Message.txt");
            this.fileWR = new StreamWriter(path, true);
            this.fileWR.Write(" " + this.GetMonthDay() + " " + this.GetDateTime() + "\r\n");
            this.fileWR.Write(msgSubject);
            this.fileWR.Write(msgBody);
            this.fileWR.Write("Done\r\n");
        }
        catch (IOException exception)
        {
            MessageBox.Show(exception.Message, "File Message");
            Application.DoEvents();
        }
        catch (Exception exception2)
        {
            MessageBox.Show(exception2.Message, "File Message");
            Application.DoEvents();
        }
        finally
        {
            this.fileWR.Flush();
            this.fileWR.Close();
            this.fileWR = null;
        }
    }

    public void WritePNDStatus(string status, string fileName)
    {
        string message = "";
        try
        {
            message = "/FlashDisk/PNDSoftware";
            message = Path.Combine(message, fileName);
            this.fileWR = new StreamWriter(message, false);
            this.fileWR.WriteLine(status);
        }
        catch (IOException exception)
        {
            message = exception.Message;
        }
        catch (Exception exception2)
        {
            message = exception2.Message;
        }
        finally
        {
            this.fileWR.Flush();
            this.fileWR.Close();
            this.fileWR = null;
        }
    }

    public void WriteResDispatchIntoMemory(string flag, string msgHeader, string Record)
    {
        string path = "";
        try
        {
            path = "/FlashDisk/PNDSoftware/Dispatch Record";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (flag == "S")
            {
                path = Path.Combine(path, "GridListShortReservedDispatch.txt");
            }
            else if (flag == "L")
            {
                path = Path.Combine(path, "GridListLongReservedDispatch.txt");
            }
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            this.fileWR = new StreamWriter(path, true);
            this.fileWR.Write(" " + msgHeader);
            this.fileWR.Write(" " + Record + "\r\n");
            this.fileWR.Write("Done\r\n");
        }
        catch (IOException exception)
        {
            string message = exception.Message;
        }
        catch (Exception exception2)
        {
            string text2 = exception2.Message;
        }
        finally
        {
            this.fileWR.Flush();
            this.fileWR.Close();
            this.fileWR = null;
        }
    }
}

 
 

}
